/*******************************************************************************************************

Table 4: Difference-in-differences estimates: time series specifications with time trends


*******************************************************************************************************/


*Set directory path
cd "C:\Users\tsa23\Documents\petitions\replication files"


clear all

*read in our main dataset
use dta/did_rep.dta

tsset ID year

*Elminate contrituencies which are not part of case G
drop if ID == 45 //University of Cambridge
drop if ID == 191 //University of Oxford
keep if new == 1 | drummond == .

drop if year==1830 | year==1831 | year==1832

gen treatgroup = 0
replace treatgroup =  1 if new==1 

gen treatinter = treatperiod*treatgroup
label var treatinter "T * A" 

// Find unique IDs
levelsof ID, local(idlist)

*borough-specific trends contruction

* Sort the data by ID and year
sort ID year

* Generate a counter for each ID, ignoring the gaps in years
by ID (year): gen counter = _n

* Generate the interaction between ID and the counter
// Loop over each unique ID and create a new variable
foreach id of local idlist {
gen time_`id' = 0
    replace time_`id' = counter if ID == `id'
}

*County-specific trends

* trend by observation order (ignores gaps)
bysort county_id (year): gen county_counter = _n   // starts at 1
* optionally start at 0:
replace county_counter = county_counter - 1

levelsof county_id, local(clist)
foreach c of local clist {
    gen time_c`c' = 0
    replace time_c`c' = county_counter if county_id == `c'
}


xtreg ln_petitions treatinter i.year, fe cluster(ID)
outreg2 treatinter using results\table4, nocons  replace addt(County trends, no, Constituency trends, no, Case, G) asterisk(coef) auto(3) label keep(treatinter ) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)

xtreg ln_petitions treatinter i.year time_c*, fe cluster(ID)
outreg2 treatinter using results\\table4, nocons  append addt(County trends, yes, Constituency trends, no, Case, G) asterisk(coef) auto(3) label keep(treatinter ) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)

xtreg ln_petitions treatinter i.year time_*, fe cluster(ID)
outreg2 treatinter using results\\table4, nocons  addt(County trends, no, Constituency trends,  yes, Case, G) asterisk(coef) auto(3) label keep(treatinter) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)


clear all

*read in our main dataset
use dta/did_rep.dta

tsset ID year


*Elminate contrituencies which are not part of case L
drop if ID == 45 //University of Cambridge
drop if ID == 191 //University of Oxford
keep if sch_A==1 | sch_B == 1 | drummond!=.

drop if year==1830 | year==1831 | year==1832

gen treatgroup = 0
replace treatgroup =  1 if sch_A==1 

gen treatinter = treatperiod*treatgroup
label var treatinter "T * A" 

// Find unique IDs
levelsof ID, local(idlist)

*borough-specific trends contruction

* Sort the data by ID and year
sort ID year

* Generate a counter for each ID, ignoring the gaps in years
by ID (year): gen counter = _n

* Generate the interaction between ID and the counter
// Loop over each unique ID and create a new variable
foreach id of local idlist {
gen time_`id' = 0
    replace time_`id' = counter if ID == `id'
}

*County-specific trends

* trend by observation order (ignores gaps)
bysort county_id (year): gen county_counter = _n   // starts at 1
* optionally start at 0:
replace county_counter = county_counter - 1

levelsof county_id, local(clist)
foreach c of local clist {
    gen time_c`c' = 0
    replace time_c`c' = county_counter if county_id == `c'
}


xtreg ln_petitions treatinter i.year, fe cluster(ID)
outreg2 treatinter using results\table4, nocons  append addt(County trends, no, Constituency trends, no, Case, L) asterisk(coef) auto(3) label keep(treatinter ) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)

xtreg ln_petitions treatinter i.year time_c*, fe cluster(ID)
outreg2 treatinter using results\table4, nocons  append addt(County trends, yes, Constituency trends, no, Case, L) asterisk(coef) auto(3) label keep(treatinter ) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)

xtreg ln_petitions treatinter i.year time_*, fe cluster(ID)
outreg2 treatinter using results\table4, nocons  addt(County trends, no, Constituency trends,  yes, Case, L) asterisk(coef) auto(3) label keep(treatinter) ctitle(Log (petitions + 1)) se  symbol(**,*,+) tex(frag)


